Xbasic

SQL::ResultSettoJSONObjectSyntax Method

Syntax

dim json as C = toJSONObjectSyntax([ RowsToCopy = -1 as N [, StartRow = -1 as N [, UpperCaseNames = .f. as L [, ConvertToText = .t. as L [, DateTimeFormat = as C [, DateFormat = as C [, ReferenceColumns as SQL::TableInfo [, UserContext as P]]]]]]]])

Arguments

RowsToCopyNumeric

number of rows to use

StartRowNumeric

first row to use

UpperCaseNamesLogical

Uppercase names if .T.

ConvertToTextLogical

If .T. (default) single-quote numeric and logical values

DateTimeFormatCharacter

Format string for dateTime values, as used by the time Function, e.g. "yyyy-MM-dd"

DateFormatCharacter

Format string for date values, as used by the time Function

ReferenceColumnsSQL::TableInfo

When data is formatted for a column in the result set: (1) if ReferenceColumns has a column with a matching name, that object will be used to format the data; (2) otherwise the ColumnInfo property of the result set is used to format the data.

UserContextPointer

The user context is passed into the evaluation of the expression when data is formatted.

Returns

jsonCharacter

Returns a string containing a JSON object.

Description

Convert a ResultSet to JSON object syntax.

Discussion

The toJSONObjectSyntax() method converts an AlphaDAO ResultSet data to a JSON object. This method is more efficient than using the rsToJSON() function.

To be consistent with the official JSON syntax, values in the generated JSON are double quoted.

Example

dim cn as SQL::Connection
? cn.open("::Name::AADemo-Northwind")
= .T.

? cn.execute("SELECT OrderID, CustomerID, OrderDate, ShippedDate FROM orders")
= .T.

txt = cn.ResultSet.toJSONObjectSyntax(10)
? txt
= {"OrderID" : "10248", "CustomerID" : "VINET", "OrderDate" : "07/04/1996 12:00:00 000 am", "ShippedDate" : "07/16/1996 12:00:00 000 am"}
{"OrderID" : "10249", "CustomerID" : "TOMSP", "OrderDate" : "07/05/1996 12:00:00 000 am", "ShippedDate" : "07/10/1996 12:00:00 000 am"}
{"OrderID" : "10250", "CustomerID" : "HANAR", "OrderDate" : "07/08/1996 12:00:00 000 am", "ShippedDate" : "07/12/1996 12:00:00 000 am"}
{"OrderID" : "10251", "CustomerID" : "VICTE", "OrderDate" : "07/08/1996 12:00:00 000 am", "ShippedDate" : "07/15/1996 12:00:00 000 am"}
{"OrderID" : "10252", "CustomerID" : "SUPRD", "OrderDate" : "07/09/1996 12:00:00 000 am", "ShippedDate" : "07/11/1996 12:00:00 000 am"}
{"OrderID" : "10253", "CustomerID" : "HANAR", "OrderDate" : "07/10/1996 12:00:00 000 am", "ShippedDate" : "07/16/1996 12:00:00 000 am"}
{"OrderID" : "10254", "CustomerID" : "CHOPS", "OrderDate" : "07/11/1996 12:00:00 000 am", "ShippedDate" : "07/23/1996 12:00:00 000 am"}
{"OrderID" : "10255", "CustomerID" : "RICSU", "OrderDate" : "07/12/1996 12:00:00 000 am", "ShippedDate" : "07/15/1996 12:00:00 000 am"}
{"OrderID" : "10256", "CustomerID" : "WELLI", "OrderDate" : "07/15/1996 12:00:00 000 am", "ShippedDate" : "07/17/1996 12:00:00 000 am"}
{"OrderID" : "10257", "CustomerID" : "HILAA", "OrderDate" : "07/16/1996 12:00:00 000 am", "ShippedDate" : "07/22/1996 12:00:00 000 am"}

ConvertToText flag

When you use the toJSONObjectSyntax() method to generate JSON data from a ResultSet, and you set the optional ConvertToText flag to .f. (it defaults to .t.), then date and date time values in the ResultSet now create Javascript date objects. For example:

dim cn as SQL::Connection
? cn.open("::Name::AADemo-Northwind")
= .T.

cn.portableSQLEnabled = .t.
? cn.execute("SELECT FIRST 5 orderid, orderdate FROM orders") 
= .T.

? cn.ResultSet.toJSONObjectSyntax(-1,-1,.f.,.f.) 
= {"OrderID" : 10248, "OrderDate" : new Date(1996, 6, 4, 0, 0, 0, 0)}
{"OrderID" : 10249, "OrderDate" : new Date(1996, 6, 5, 0, 0, 0, 0)}
{"OrderID" : 10250, "OrderDate" : new Date(1996, 6, 8, 0, 0, 0, 0)}
{"OrderID" : 10251, "OrderDate" : new Date(1996, 6, 8, 0, 0, 0, 0)}
{"OrderID" : 10252, "OrderDate" : new Date(1996, 6, 9, 0, 0, 0, 0)}

See Also